let currentPage = 1;
const pageSize = 5;


document.addEventListener('DOMContentLoaded', () => {
    loadBooks();
});

async function loadBooks(keyword = '') {
    try {

        let url = `${BASE_API}?page=${currentPage}&size=${pageSize}`;
        if (keyword) url = `${BASE_API}/search?keyword=${encodeURIComponent(keyword)}`;

        const response = await getRequest(url);
        const { data } = response;
        const books = keyword ? data : data.books;

        const tableBody = document.getElementById('bookTableBody');
        tableBody.innerHTML = '';

        if (books.length === 0) {
            tableBody.innerHTML = '<tr><td colspan="7" class="text-center">暂无图书数据</td></tr>';
            return;
        }

        books.forEach(book => {
            const row = document.createElement('tr');
            row.innerHTML = `
                <td>${book.id}</td>
                <td>${book.title}</td>
                <td>${book.author}</td>
                <td>${book.isbn}</td>
                <td>${book.category}</td>
                <td>${book.availableCopies}/${book.totalCopies}</td>
                <td>
                    <button class="btn btn-sm btn-warning" onclick="editBook(${book.id})">
                        <i class="bi bi-pencil"></i> 编辑
                    </button>
                    <button class="btn btn-sm btn-danger" onclick="deleteBook(${book.id})">
                        <i class="bi bi-trash"></i> 删除
                    </button>
                </td>
            `;
            tableBody.appendChild(row);
        });


        if (!keyword) {
            renderPagination(data.totalPages, currentPage);
        }
    } catch (error) {
        console.error('加载图书失败：', error);
    }
}

function searchBooks() {
    const keyword = document.getElementById('searchInput').value.trim();
    loadBooks(keyword);
}

function resetSearch() {
    document.getElementById('searchInput').value = '';
    currentPage = 1;
    loadBooks();
}

function editBook(id) {
    window.location.href = `edit-book.html?id=${id}`;
}

async function deleteBook(id) {
    if (confirm('确定要删除这本图书吗？')) {
        try {
            await deleteRequest(`${BASE_API}/${id}`);
            alert('删除成功');
            loadBooks();
        } catch (error) {
            console.error('删除失败：', error);
        }
    }
}

function renderPagination(totalPages, currentPage) {
    const pagination = document.getElementById('pagination');
    pagination.innerHTML = '';

    pagination.innerHTML += `
        <li class="page-item ${currentPage === 1 ? 'disabled' : ''}">
            <a class="page-link" onclick="changePage(${currentPage - 1})" href="#">上一页</a>
        </li>
    `;

    for (let i = 1; i <= totalPages; i++) {
        pagination.innerHTML += `
            <li class="page-item ${i === currentPage ? 'active' : ''}">
                <a class="page-link" onclick="changePage(${i})" href="#">${i}</a>
            </li>
        `;
    }

    pagination.innerHTML += `
        <li class="page-item ${currentPage === totalPages ? 'disabled' : ''}">
            <a class="page-link" onclick="changePage(${currentPage + 1})" href="#">下一页</a>
        </li>
    `;
}

function changePage(page) {
    currentPage = page;
    loadBooks();
}